//
//  AppDelegate.swift
//  HMWeibo12
//
//  Created by iOS on 16/9/16.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setupAppearance()
        HMSQLiteManager.shared
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        
        if HMUserAccountVM.shared.isUserLogin {
            print("已经登录了: \(HMUserAccountVM.shared.userAccount)")
        } else {
            print("没有登录")
        }
        
        // 设置window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        // 设置根控制器
//        let tabbarVC = HMMainTabBarController()
//        tabbarVC.view.backgroundColor = UIColor.brown
        
        window?.rootViewController = defaultRootViewController()
        
        // 测试某个控制器
//        window?.rootViewController = HMNewFeatureController()
        
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    /// 不需要干预就能确定根控制器
    private func defaultRootViewController() -> UIViewController {
        if !HMUserAccountVM.shared.isUserLogin {
            // 没有登录返回 HMMainTabBarController
            return HMMainTabBarController()
        }
        
        // 登录了
        if checkNewVersion() {
            // 新版本
            return HMNewFeatureController()
        } else {
            // 不是新版本
            return HMWelcomeViewController()
        }
    }
    
    /// 是否是新版本, 只能调一次
    private func checkNewVersion() -> Bool {
        // 获取当前版本号
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        
        let sandBoxVersionKey = "sandBoxVersionKey"
        // 获取上一次运行时的版本号
        let sandBoxVersion = UserDefaults.standard.string(forKey: sandBoxVersionKey)
        
        // 对比版本号
        // 1.0.2 != 1.0.2 false
        let isNewVersion = currentVersion != sandBoxVersion
        if isNewVersion {
            // 如果是新版本,保存最新的版本号
            UserDefaults.standard.set(currentVersion, forKey: sandBoxVersionKey)
            UserDefaults.standard.synchronize()
        }
        
        // 返回
        return isNewVersion
    }
    
    /// 设置导航栏的皮肤(全局的都会被改变)
    private func setupAppearance() -> Void {
        let navi = UINavigationBar.appearance()
        navi.tintColor = UIColor.orange
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // 程序进入后台,来清理微博数据
        HMStatusDAL().clearCacheStatus()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        
    }
}

